
package CCT220_Trabalho04_13106.control;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

import CCT220_Trabalho04_13106.model.Assentamento;
import CCT220_Trabalho04_13106.model.Disciplina;
import CCT220_Trabalho04_13106.model.Estudante;
import CCT220_Trabalho04_13106.view.LimiteAssentamento;


/**
 * </br>******************************
 * </br>Project: CCT220
 * </br>Package: CCT220_Trabalho04_13106.control
 * </br>Class: ControleAssentamento
 * </br>Last Change: 08/10/2011 11:20:43 by marcel
 * </br>Description: Class that controls the operations for the entity
 * Assentamento
 * </br>******************************</br>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class ControleAssentamento {
	/**
	 * ControlePrincipal
	 */
	private final ControlePrincipal  ctrPrincipal;
	/**
	 * Vector<Assentamento>
	 */
	private Vector<Assentamento>     listaAssentamento;
	/**
	 * LimiteAssentamento
	 */
	private final LimiteAssentamento limAssentamento;


	/**
	 * </br>******************************
	 * </br>Constructor for
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>LastChange: 08/10/2011 11:20:45 by marcel
	 * </br>******************************</br>
	 * 
	 * @param controlePrincipal
	 * @throws Exception
	 */
	public ControleAssentamento(ControlePrincipal controlePrincipal)
	        throws Exception {

		this.ctrPrincipal = controlePrincipal;
		this.listaAssentamento = new Vector<Assentamento>();
		this.limAssentamento = new LimiteAssentamento(this);
		this.desserializaAssentamento();
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: run
	 * </br>Last Change: 08/10/2011 11:20:48 by marcel
	 * </br>Description: Method that call the menu and manage the users options
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	public void run() throws Exception {

		int option = -1;
		while (true) {
			option = this.limAssentamento.montaMenu();
			if (option == 5) {
				this.serializaAssentamento();
				break;
			}
			switch (option) {
				case 1:
					this.cadastra();
					break;
				case 2:
					this.listaUm();
					break;
				case 3:
					this.listaTudo();
					break;
				case 4:
					this.removeUm();
					break;
			}
		}
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: cadastra
	 * </br>Last Change: 08/10/2011 11:20:52 by marcel
	 * </br>Description: Method that call the data insertion and insert the
	 * values in the list
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	private void cadastra() throws Exception {

		this.adicionaNaLista(limAssentamento.capturaDadosCadastrais(
		        this.ctrPrincipal.getControleEstudante().getList(),
		        this.ctrPrincipal.getControleDisciplina().getList()));
		this.serializaAssentamento();

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: listaUm
	 * </br>Last Change: 08/10/2011 11:20:54 by marcel
	 * </br>Description: Method that show just one element from the list
	 * </br>******************************</br>
	 * 
	 */
	private void listaUm() {

		int selectedOne = -1;
		do {
			selectedOne = this.limAssentamento.selectOne(this.getList());
		} while (!this.isInList(selectedOne));
		this.limAssentamento.show(this.getFromList(selectedOne).toString());

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: listaTudo
	 * </br>Last Change: 08/10/2011 11:20:57 by marcel
	 * </br>Description: Method that show all elements from the list
	 * </br>******************************</br>
	 * 
	 */
	private void listaTudo() {

		this.limAssentamento.showAll(this.getList());

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: removeUm
	 * </br>Last Change: 08/10/2011 11:21:00 by marcel
	 * </br>Description: Method that removes one element from the list
	 * </br>******************************</br>
	 * 
	 */
	private void removeUm() {

		Integer selectedOne = -1;
		do {
			selectedOne = this.limAssentamento.selectOne(this.getList());
		} while (!this.isInList(selectedOne));
		if (selectedOne != null) {
			String description = this.getFromList(selectedOne).toString();
			this.listaAssentamento.remove(this.getFromList(selectedOne));
			this.limAssentamento.show(description + " foi removida!");
		}

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: adicionaNaLista
	 * </br>Last Change: 08/10/2011 11:21:02 by marcel
	 * </br>Description: Method that add an element to the list
	 * </br>******************************</br>
	 * 
	 * @param a
	 */
	private void adicionaNaLista(Assentamento a) {

		this.listaAssentamento.add(a);

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: getFromList
	 * </br>Last Change: 08/10/2011 11:21:05 by marcel
	 * </br>Description: Method that get one element from the list by the given
	 * selected element code
	 * </br>******************************</br>
	 * 
	 * @param selectedOne
	 * @return Assentamento
	 */
	private Assentamento getFromList(int selectedOne) {

		Iterator<Assentamento> it = this.listaAssentamento.iterator();
		while (it.hasNext()) {
			Assentamento a = it.next();
			if (a.getCodigoAssentamento() == selectedOne) { return a; }
		}
		return null;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: isInList
	 * </br>Last Change: 08/10/2011 11:21:08 by marcel
	 * </br>Description: Method that verifies if the given selected element code
	 * is in the list
	 * </br>******************************</br>
	 * 
	 * @param selectedOne
	 * @return true | false
	 */
	private boolean isInList(int selectedOne) {

		Iterator<Assentamento> it = this.listaAssentamento.iterator();
		while (it.hasNext()) {
			if (it.next().getCodigoAssentamento() == selectedOne) { return true; }
		}
		return false;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: getList
	 * </br>Last Change: 08/10/2011 11:21:10 by marcel
	 * </br>Description: Method that get the whole list in a
	 * LinkedList<String> form
	 * </br>******************************</br>
	 * 
	 * @return LinkedList<String>
	 */
	private LinkedList<String> getList() {

		LinkedList<String> lista = new LinkedList<String>();
		Iterator<Assentamento> it = this.listaAssentamento.iterator();
		while (it.hasNext()) {
			lista.add(it.next().toString());
		}
		return lista;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: isInListEstudante
	 * </br>Last Change: 08/10/2011 11:21:13 by marcel
	 * </br>Description: Method that verifies if the given selected element code
	 * is in the list of Estudante
	 * </br>******************************</br>
	 * 
	 * @param choice
	 * @return true | false
	 */
	public boolean isInListEstudante(int choice) {

		if (ctrPrincipal.getControleEstudante().getFromList(choice) != null) { return true; }
		return false;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: getInListEstudante
	 * </br>Last Change: 08/10/2011 11:21:16 by marcel
	 * </br>Description:Method get the element Estudante by the given selected
	 * element code is in the list of Estudante
	 * </br>******************************</br>
	 * 
	 * @param choice
	 * @return Estudante | null
	 */
	public Estudante getInListEstudante(int choice) {

		return ctrPrincipal.getControleEstudante().getFromList(choice);
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: isInListDisciplina
	 * </br>Last Change: 08/10/2011 11:21:19 by marcel
	 * </br>Description: Method that verifies if the given selected element code
	 * is in the list of Disciplina
	 * </br>******************************</br>
	 * 
	 * @param choice
	 * @return true | false
	 */
	public boolean isInListDisciplina(int choice) {

		if (ctrPrincipal.getControleDisciplina().getFromList(choice) != null) { return true; }
		return false;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: getInListDisciplina
	 * </br>Last Change: 08/10/2011 11:21:22 by marcel
	 * </br>Description: Method get the element Disciplina by the given selected
	 * element code is in the list of Disciplina
	 * </br>******************************</br>
	 * 
	 * @param choice
	 * @return Disciplina | null
	 */
	public Disciplina getInListDisciplina(int choice) {

		return ctrPrincipal.getControleDisciplina().getFromList(choice);
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: serializaAssentamento
	 * </br>Last Change: 08/10/2011 11:21:25 by marcel
	 * </br>Description: Method that serializes the list of elements to a file
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	private void serializaAssentamento() throws Exception {

		FileOutputStream fos = new FileOutputStream("assentamentos.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(this.listaAssentamento);
		oos.flush();
		oos.close();
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleAssentamento
	 * </br>Method: desserializaAssentamento
	 * </br>Last Change: 08/10/2011 11:21:30 by marcel
	 * </br>Description: Method that desserializes the list of elements from a
	 * file
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private void desserializaAssentamento() throws Exception {

		File file = new File("assentamentos.dat");
		if (file.exists()) {
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			this.listaAssentamento = (Vector<Assentamento>) ois.readObject();
			ois.close();
		}

	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#finalize()
	 */
	@Override
	public void finalize() throws Exception {

		this.serializaAssentamento();
	}

}
